package org.zoomdev.zoom.common.caster;

import java.lang.reflect.Type;

public final class TypeKey {
    final Class<?> source;
    final Type target;
    private final int hash; // 预计算哈希值

    // 私有构造器 + 工厂方法（实现缓存）
    private TypeKey(Class<?> source, Type target) {
        this.source = source;
        this.target = target;
        this.hash = source.hashCode() * 31 + target.hashCode();
    }

    public static TypeKey of(Class<?> source, Type target) {
//        return CACHE.computeIfAbsent(source, k -> new ConcurrentHashMap<>())
//                .computeIfAbsent(target, k -> new TypeKey(source, target));
        return new TypeKey(source, target);
    }

    @Override
    public int hashCode() {
        return hash;
    }

    // 缓存池（避免重复创建）
    //  private static final ConcurrentMap<Class<?>, ConcurrentMap<Type, TypeKey>> CACHE = new ConcurrentHashMap<>();

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        TypeKey other = (TypeKey) obj;
        return source == other.source && target == other.target;
    }
}